package com.foodlocker.courier;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

import com.foodlocker.bean.Courier;
import com.foodlocker.bean.Order;
import com.foodlocker.common.CommonFunction;
import com.foodlocker.common.Constant;
import com.foodlocker.common.DateTimeFunction;
import com.foodlocker.common.ListViewUtil;
import com.foodlocker.common.UniversalHttpPost;
import com.foodlocker.machine.Box;
import com.foodlocker.machine.MainActivity;
import com.foodlocker.machine.R;
import com.foodlocker.sqlite.OrderDao;



import android.app.Activity;
import android.app.Instrumentation;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.JsonReader;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class DepositAct extends Activity {

	 JSONArray orderJA= new JSONArray();
	 JSONArray orderToJA= new JSONArray();
	 ListView listView,listView_toArrive;
	 RelativeLayout rt_shadow;
	 EditText et_deposit_id;
	 MyListAdapter myListAdapter;
	 ToListAdapter toListAdapter;
	 Button btn_confirm_deposit,btn_toMainAct_deposit,btn_toSee;
	 TextView tv_hint_deposit,tv_order_to,tv_title;
	 DepositKeyboard keyboard;
	 String AFFAIR_ADDBOX="addBox";
	 String AFFAIR_DELETEBOX="deleteBox";
	 String AFFAIR_LOCKBOX="lockBoxInUsing";
	 String AFFAIR_ORDERARRIVE="orderArrive";
	 CheckBox ck_showkeyboard;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);	
		setContentView(R.layout.activty_deposit);
		tv_hint_deposit= (TextView) findViewById(R.id.tv_hint_deposit);
		tv_order_to=  (TextView) findViewById(R.id.tv_ordertoarrive_deposit);
		tv_title=(TextView) findViewById(R.id.tv_title_deposit);
		listView= (ListView) findViewById(R.id.listview_inputed_deposit);
		listView_toArrive=(ListView) findViewById(R.id.listview_ordertoarrive_deposit);	
		rt_shadow= (RelativeLayout) findViewById(R.id.rt_shadow_actdeposit);
		rt_shadow.setVisibility(View.GONE);
	//	orderJA=updateFromLocalDB();
		myListAdapter=new MyListAdapter(DepositAct.this);
		listView.setAdapter(myListAdapter);		
		et_deposit_id= (EditText) findViewById(R.id.et_orderid_deposit);
		
		keyboard= new DepositKeyboard(this,DepositAct.this, R.id.keyboard_deposit);
		keyboard.setEditText(et_deposit_id);
		// keyboard.showKeyboard();
		ck_showkeyboard= (CheckBox) findViewById(R.id.ck_showkeybord_deposit);	
		orderToJA= new OrderDao(DepositAct.this).queryToArriveOrder();
		tv_order_to.setText("待提交订单 数量："+ orderToJA.size());
		toListAdapter= new ToListAdapter(DepositAct.this);
		listView_toArrive.setAdapter(toListAdapter);	
		View.OnFocusChangeListener  focusListener= new View.OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if(hasFocus==true){
					if(v==et_deposit_id){
						keyboard.setEditText(et_deposit_id);
					}			
				}		
			}
		};
		et_deposit_id.addTextChangedListener(new MyTextWatcher());
		et_deposit_id.setOnFocusChangeListener(focusListener);
		
		btn_confirm_deposit= (Button) findViewById(R.id.btn_confirm_deposit);
		btn_confirm_deposit.setOnClickListener(new View.OnClickListener() {
	  
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			if(orderJA.isEmpty())  tv_hint_deposit.setText("请先录入订单");
			else{
			String[] params= new String[3];
	         params[0]=Constant.BaseUrl+Constant.MachineUrl;
	         params[1]=AFFAIR_ORDERARRIVE; //PostCod
	         JSONObject jsonData=new JSONObject();
	         jsonData.put("machine", Constant.machineJO);
	         jsonData.put("orders",orderJA);
	         jsonData.put("courier",Constant.courierJO.getString(Courier.TELNUM));
	         params[2]=jsonData.toString();
	         rt_shadow.setVisibility(View.VISIBLE);
	         new HttpPostTask().execute(params);
	          }
			}
		});
		
		btn_toMainAct_deposit= (Button) findViewById(R.id.btn_toMainAct_deposit);
		btn_toMainAct_deposit.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				new Thread () { 
					public void run () { 
					try { 									
					Instrumentation inst= new Instrumentation(); 
					inst.sendKeyDownUpSync(KeyEvent. KEYCODE_BACK); 
					} catch(Exception e) { 
					e.printStackTrace(); 
					         } 
					  } 
					}.start();							
			}
		});
		
		btn_toSee= (Button) findViewById(R.id.btn_seeordertoarrive_deposit);
		btn_toSee.setOnClickListener(new View.OnClickListener() {
			  
			@Override
			public void onClick(View v) {
			  if(listView.isShown()){
				  listView.setVisibility(View.GONE);
				  listView_toArrive.setVisibility(View.VISIBLE);
				  btn_toSee.setText("继续录入");
				  tv_title.setText("待提交订单");
			  }else{
				  listView.setVisibility(View.VISIBLE);
				  listView_toArrive.setVisibility(View.GONE);
				  btn_toSee.setText("查看");
				  tv_title.setText("已录入订单"); 
			  }
			  
			}
		});
		
		ck_showkeyboard.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					keyboard.showKeyboard();
					ck_showkeyboard.setText("显示键盘");
				}else{
					keyboard.hideKeyboard();
					ck_showkeyboard.setText("隐藏键盘");
				}
			}
		});
	}
	
	 class MyTextWatcher implements TextWatcher{
         
		@Override
		public void afterTextChanged(Editable order_id) {
			
			// TODO Auto-generated method stub
			String s=order_id.toString();
			s.replaceAll("\n", "\\\\n");
			s.replaceAll("\r", "\\\\r");
			System.out.println(s);
			if(order_id.toString().contains("\r")||order_id.toString().contains("\n")){
				String orderID = order_id.toString().replaceAll("\r|\n", "");
				tv_hint_deposit.setText("输入订单号为:"+orderID+"\n");
			  JSONObject orderJO =new OrderDao(DepositAct.this).queryById(orderID);
				if(orderJO.getBoolean("success")){
					long timeAE= Long.parseLong(orderJO.getString(Order.DATETIMEAE));
					long timeTL= Long.parseLong(orderJO.getString(Order.DATETIMETL));
					long timeNow= Constant.CURRENT_TIME_SECOND;
					if(timeNow<timeAE||timeNow>timeTL){
						System.out.println(" dateTimeAE= "+orderJO.getString(Order.DATETIMEAE) +" dateTimeTL= "+orderJO.getString(Order.DATETIMETL)+"timeNow="+timeNow);
						String noteStr="订单："+ orderJO.getLong(Order.ID)+" 最早送达时间"+DateTimeFunction.transWordDateFormat(orderJO.getString(Order.DATETIMEAE));
						noteStr+=" 最迟送达时间"+DateTimeFunction.transWordDateFormat(orderJO.getString(Order.DATETIMETL));
						noteStr+="目前不在该时间段，不能送货";
						CommonFunction.alert(DepositAct.this, "提示", noteStr);return;
					}
					int orderState=orderJO.getInt(Order.STATE);	
					if(orderState<=4){
						String noteStr="订单："+ orderJO.getLong(Order.ID)+" 未支付，不能送货";	
						CommonFunction.alert(DepositAct.this, "提示", noteStr);return;
					}
					if(orderState<10){
						String noteStr="订单："+ orderJO.getLong(Order.ID)+" 已拒绝接单或已退款，不需要送货";	
						CommonFunction.alert(DepositAct.this, "提示", noteStr);return;
					}
					if(orderState>21){
						String noteStr="订单："+ orderJO.getLong(Order.ID)+" 已送达，请不要重复提交";	
						CommonFunction.alert(DepositAct.this, "提示", noteStr);return;
					}
					  addOrderToListView(orderJO);
					String boxesIn=orderJO.getString(Order.BOXESIN);
					String noteStr ="订单："+ orderJO.getLong(Order.ID)+"  该订单预放在: ";
					List<Integer> boxes= new ArrayList<Integer>();
					 for(int i=0;i<boxesIn.length()/3;i++){
						 int boxID=Integer.parseInt(boxesIn.substring(i*3,(i+1)*3));
						boxes.add(boxID);
						noteStr+=boxID +"号  ";
					 }
					tv_hint_deposit.setText(noteStr+"箱格中\n");
					      
					         Message msg = new Message();              
			                 msg.what=1;
			                 msg.obj=boxes;
			                 Box.childHander.sendMessage(msg);
			                 Log.i("DepositActivity", " Send a message to the child thread -msg.what= "
			                 +msg.what+ " smg.obj=" + msg.obj);	
			             						
					
					
				}else { tv_hint_deposit.setText(tv_hint_deposit.getText()+ "该订单无效\n"+"可能商家尚未接单\n"); }
			
				order_id.clear();
			}
			
		}

		@Override
		public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
				int arg3) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onTextChanged(CharSequence arg0, int arg1, int arg2,
				int arg3) {
			// TODO Auto-generated method stub
			
		}
		 
	 }
	
	 private void addOrderToListView( JSONObject orderJO ){
		 long orderID= orderJO.getLong(Order.ID);
		 for(int i=0;i< orderJA.size();i++){
			 if(orderJA.getJSONObject(i).getLong(Order.ID)==orderID)
				 return;
		 }
		 orderJA.add(orderJO);
		 myListAdapter.notifyDataSetChanged();	
	 }

	 private class MyListAdapter extends BaseAdapter {
		
		    private LayoutInflater layoutInflater;  
		    private Context context;  
		    
		public MyListAdapter(Context context) {
				super();
				this.context = context;
			    this.layoutInflater=LayoutInflater.from(context); 
			}
		
		
		final class Zujian{  
		    public TextView orderid,customer;  
	      
	       public Button addbox;  
	       public ListView listView;
	     
	    }   //end of Zujian

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			 return orderJA.size();  
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			 return orderJA.getJSONObject(position);  
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position; 
		}
	  
		@Override
		public View getView(int position, View  convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			final int selectID = position;   
			 Zujian zujian=null;  
		        if(convertView==null){  
		            zujian=new Zujian();  
		            //获得组件，实例化组件  
		            convertView=layoutInflater.inflate(R.layout.listlayout_deposit, null);  
         
		            zujian.orderid=(TextView) convertView.findViewById(R.id.tv_orderid_listparent_deposit);
		            zujian.addbox=(Button) convertView.findViewById(R.id.btn_add_listparent_deposit);
		            zujian.listView= (ListView) convertView.findViewById(R.id.list__listparent_deposit);
		            zujian.customer=(TextView) convertView.findViewById(R.id.tv_customer_listparent_deposit);           
		            convertView.setTag(zujian);  
		            System.out.println("position----"+position+"------inflated");
		        }else{   System.out.println("position----"+position+"-------not inflated");
		            zujian=(Zujian)convertView.getTag();  
		        }  
		        //绑定数据  
		      //  zujian.image.setBackgroundResource((Integer)data.getJSONObject(position).get("image"));  
		      
		        zujian.orderid.setText("订单号："+orderJA.getJSONObject(position).getLong(Order.ID));
		        zujian.customer.setText("客户："+orderJA.getJSONObject(position).getLong(Order.CUSTOMERTELNUM)); 
		        String boxesIn= orderJA.getJSONObject(position).getString(Order.BOXESIN);
		        zujian.listView.setAdapter( new ChildListAdapter(context, boxesIn,selectID));
		        ListViewUtil.setListViewHeightBasedOnChildren(zujian.listView);
		         zujian.addbox.setOnClickListener(new View.OnClickListener() {		
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					String[] params= new String[3];
			         params[0]=Constant.BaseUrl+Constant.MachineUrl;
			         params[1]=AFFAIR_ADDBOX; //PostCod
			         JSONObject jsonData=new JSONObject();
			         jsonData.put("machine", Constant.machineJO);
			         JSONObject orderTempJO= new JSONObject();
			         orderTempJO.put(Order.ID,orderJA.getJSONObject(selectID).getLong(Order.ID));
			         jsonData.put("order", orderTempJO);
			         params[2]=jsonData.toString();
			         rt_shadow.setVisibility(View.VISIBLE);
			      new HttpPostTask().execute(params);
		                 } 
			});
		     
		        return convertView;  
		} // end of getview()
		
		
		
		
	} //end of Adapter
	 
	 private class ToListAdapter extends BaseAdapter {
			
		    private LayoutInflater layoutInflater;  
		    private Context context;  
		    
		public ToListAdapter(Context context) {
				super();
				this.context = context;
			    this.layoutInflater=LayoutInflater.from(context); 
			}
		
		
		final class Zujian{  
		    public TextView orderid,customer;
		    public Button addbox;  
		       public ListView listView;
     
	    }   //end of Zujian

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			 return orderToJA.size();  
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			 return orderToJA.getJSONObject(position);  
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position; 
		}
	  
		@Override
		public View getView(int position, View  convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			final int selectID = position;   
			 Zujian zujian=null;  
		        if(convertView==null){  
		            zujian=new Zujian();  
		            //获得组件，实例化组件  
		            convertView=layoutInflater.inflate(R.layout.listlayout_deposit, null);  
      
		            zujian.orderid=(TextView) convertView.findViewById(R.id.tv_orderid_listparent_deposit);
		            zujian.customer=(TextView) convertView.findViewById(R.id.tv_customer_listparent_deposit);           
		            zujian.addbox=(Button) convertView.findViewById(R.id.btn_add_listparent_deposit);
		            zujian.listView= (ListView) convertView.findViewById(R.id.list__listparent_deposit);
		            convertView.setTag(zujian);  
		            System.out.println("position----"+position+"------inflated");
		        }else{   System.out.println("position----"+position+"-------not inflated");
		            zujian=(Zujian)convertView.getTag();  
		        }  
		        //绑定数据  
		      //  zujian.image.setBackgroundResource((Integer)data.getJSONObject(position).get("image"));  
		      
		        zujian.orderid.setText("订单号："+orderToJA.getJSONObject(position).getLong(Order.ID)); 
		        zujian.customer.setText("客户："+orderToJA.getJSONObject(position).getLong(Order.CUSTOMERTELNUM)); 
                zujian.addbox.setVisibility(View.GONE);
                zujian.listView.setVisibility(View.GONE);
		        return convertView;  
		} // end of getview()
		
		
		
		
	} //end of Adapter
	 
	 private  class HttpPostTask extends android.os.AsyncTask<String,String,Map<String,Object>>{

			

			@Override
			protected Map<String,Object> doInBackground(String... params) {
				// TODO Auto-generated method stub
				 Map<String,Object> map = new HashMap<String ,Object>();		
			  map =	UniversalHttpPost.doPost(params);	             
				return map;
			}
			
			@Override
			protected void onPostExecute(Map<String,Object> map) {
					
				 System.out.println("-----------onPostExecute()--------------");
				 rt_shadow.setVisibility(View.GONE);
				 String PostResult=(String) map.get("PostResult");		 
				 boolean networkstate=(Boolean) map.get("PostState");
				 if(!networkstate){
					 CommonFunction.alert(DepositAct.this, "失败", "无网络连接");return;
			      
				}
				//	Toast.makeText(DepositAct.this, "服务器返回数据为"+PostResult, Toast.LENGTH_SHORT).show();
				    JSONObject json = null;
				    int resultCode=-1;
					String resultInfo="";
					String affair="";				
					JSONObject jo;
					try{
				     json= JSONObject.fromObject(PostResult);
				     resultCode=json.getInt("resultCode");
				     resultInfo=json.getString("resultInfo");	
				     affair =json.getString("affair");	
				     jo= json.getJSONObject("jo");
					}catch(JSONException e){
						 CommonFunction.alert(DepositAct.this, "失败", "数据解析错误");return;
					}
					if(affair.equals(AFFAIR_ADDBOX)){
						if(resultCode==2){
							String boxAdded=jo.getString("boxid");
							long orderid= jo.getLong("orderid");
							for(int i=0;i<orderJA.size();i++){
								if(orderJA.getJSONObject(i).getLong(Order.ID)==orderid){
									String boxesIn= orderJA.getJSONObject(i).getString(Order.BOXESIN);
									orderJA.getJSONObject(i).put(Order.BOXESIN, boxesIn+boxAdded);
									myListAdapter.notifyDataSetChanged();
									//更改数据库
								     new OrderDao(DepositAct.this).updateBoxesIn(orderid, orderJA.getJSONObject(i).getString(Order.BOXESIN));
								   //打开新增的箱格
								     List<Integer> boxes= new ArrayList<Integer>();
								     boxes.add(Integer.parseInt(boxAdded));
								     Message msg = new Message();              
					                 msg.what=1;
					                 msg.obj=boxes;
					                 Box.childHander.sendMessage(msg);
					                
								     break;
								}
							}
							 CommonFunction.alert(DepositAct.this, "成功", "新增箱格成功，箱格号："+boxAdded);return;	 
						}else{
							 CommonFunction.alert(DepositAct.this, "失败", resultInfo);return;	
						}					
					}
					if(affair.equals(AFFAIR_DELETEBOX)||affair.equals(AFFAIR_LOCKBOX)){
						if(resultCode==2){
							String boxSubtracted=jo.getString("boxid");
							long orderid= jo.getLong("orderid");
							String boxesInNew="";
							for(int i=0;i<orderJA.size();i++){
								if(orderJA.getJSONObject(i).getLong(Order.ID)==orderid){
									String boxesIn= orderJA.getJSONObject(i).getString(Order.BOXESIN);					
								    for( int j=0;j<boxesIn.length()/3;j++){
								    	String boxStr= boxesIn.substring(j*3, (j+1)*3);
								    	if(boxStr.equals(boxSubtracted)){
								    		boxesInNew= boxesIn.substring(0,j*3)+boxesIn.substring((j+1)*3);
								    		break;
								    	}
								    }	
								    		
									orderJA.getJSONObject(i).put(Order.BOXESIN, boxesInNew);
									myListAdapter.notifyDataSetChanged();
									//更改数据库
								     new OrderDao(DepositAct.this).updateBoxesIn(orderid, orderJA.getJSONObject(i).getString(Order.BOXESIN));
									break;
								}
							}
							 CommonFunction.alert(DepositAct.this, "成功", resultInfo);return;	 
						}else{
							 CommonFunction.alert(DepositAct.this, "失败", resultInfo);return;	
						}					
					}	
					if(affair.equals(AFFAIR_ORDERARRIVE)){
						if(resultCode==2){
							JSONArray orderArrived= jo.getJSONArray("orders");
							for(int j=0;j<orderArrived.size();j++){
								long orderid= orderArrived.getJSONObject(j).getLong(Order.ID);
							for(int i=0;i<orderJA.size();i++){
								if(orderJA.getJSONObject(i).getLong(Order.ID)==orderid){
									 orderJA.remove(i);									
									//更改数据库
								     new OrderDao(DepositAct.this).updateOrderState(orderid, 21);
									break;
								      }
							       }
						         }
							 CommonFunction.alert(DepositAct.this, "成功", resultInfo);
							 myListAdapter.notifyDataSetChanged();
							 orderToJA= new OrderDao(DepositAct.this).queryToArriveOrder();
							 toListAdapter.notifyDataSetChanged();
							 tv_order_to.setText("待提交订单 数量："+ orderToJA.size());
							 return;
						}else{
							 CommonFunction.alert(DepositAct.this, "失败", resultInfo);return;	
						}					
					}
			       
				
			} //onPostExecute
		} //AsyncTask
	
	 
	 private class ChildListAdapter extends BaseAdapter {
			
		    private LayoutInflater layoutInflater;  
		    private Context context;  
		    private String boxesIn;
		    int parentPosition;
		public ChildListAdapter (Context context, String boxesIn,int parentPosition) {
				super();
				this.context = context;
			    this.layoutInflater=LayoutInflater.from(context); 
			    this.boxesIn= boxesIn;
			    this.parentPosition= parentPosition;
			}
		
		
		final class Zujian{  
		    public TextView boxid;  
	      
	       public Button reopen,lock,delete; 
	     
	    }   //end of Zujian

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return boxesIn.length()/3;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			 return position; 
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position; 
		}
	  
		@Override
		public View getView(int position, View  convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			final int selectID = position;   
			 Zujian zujian=null;  
		        if(convertView==null){  
		            zujian=new Zujian();  
		            //获得组件，实例化组件  
		            convertView=layoutInflater.inflate(R.layout.list_child_deposit, null);  
	           
		            zujian.boxid=(TextView) convertView.findViewById(R.id.tv_boxid_listchild_actdeposit);
		            zujian.reopen=(Button) convertView.findViewById(R.id.btn_reopen_listchild_actdeposit);
		            zujian.lock=(Button) convertView.findViewById(R.id.btn_lock_listchild_actdeposit);
		            zujian.delete=(Button) convertView.findViewById(R.id.btn_delete_listchild_actdeposit);
		            convertView.setTag(zujian);  
		            System.out.println("position----"+position+"------inflated");
		        }else{   System.out.println("position----"+position+"-------not inflated");
		            zujian=(Zujian)convertView.getTag();  
		        }  
		        //绑定数据  
		      //  zujian.image.setBackgroundResource((Integer)data.getJSONObject(position).get("image"));  
		           final int boxId=Integer.parseInt(boxesIn.substring(selectID*3, (selectID+1)*3));
		        zujian.boxid.setText("箱号："+boxId ); 
		         
		     
		      zujian.reopen.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
				
					 Message msg = new Message();              
	                 msg.what=1;
	                 List<Integer> boxes=new ArrayList<Integer>();
	                 boxes.add(boxId);
	                 msg.obj=boxes;
	                 Box.childHander.sendMessage(msg);
	                 Log.i("DepositActivity", " Send a message to the child thread -msg.what= "
	                 +msg.what+ " smg.obj=" + msg.obj);	
								
		                 } 
			});
		      zujian.delete.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						String[] params= new String[3];
				         params[0]=Constant.BaseUrl+Constant.MachineUrl;
				         params[1]=AFFAIR_DELETEBOX; //PostCod
				         JSONObject jsonData=new JSONObject();
				         jsonData.put("machine", Constant.machineJO);
				         JSONObject orderTempJO= new JSONObject();
				         orderTempJO.put(Order.ID,orderJA.getJSONObject(parentPosition).getLong(Order.ID));
				         String boxStr=""+boxId;
				         while(boxStr.length()<3){
				        	 boxStr="0"+boxStr;
				         }
				         orderTempJO.put(Order.BOXESIN,boxStr);
				         jsonData.put("order", orderTempJO);
				         params[2]=jsonData.toString();
				         rt_shadow.setVisibility(View.VISIBLE);
				      new HttpPostTask().execute(params);
									
			                 } 
				}); 
		      zujian.lock.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						String[] params= new String[3];
				         params[0]=Constant.BaseUrl+Constant.MachineUrl;
				         params[1]=AFFAIR_LOCKBOX; //PostCod
				         JSONObject jsonData=new JSONObject();
				         jsonData.put("machine", Constant.machineJO);
				         JSONObject orderTempJO= new JSONObject();
				         orderTempJO.put(Order.ID,orderJA.getJSONObject(parentPosition).getLong(Order.ID));
				         String boxStr=""+boxId;
				         while(boxStr.length()<3){
				        	 boxStr="0"+boxStr;
				         }
				         orderTempJO.put(Order.BOXESIN,boxStr);
				         jsonData.put("order", orderTempJO);
				         params[2]=jsonData.toString();
				         rt_shadow.setVisibility(View.VISIBLE);
				      new HttpPostTask().execute(params);
									
			                 } 
				}); 
		        return convertView;  
		} // end of getview()
	 }
}
